package com.ytjj.qmyx.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.constants.RedisKeyConstant;
import com.ytjj.common.dto.mqdto.ClickStatisticsDto;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.data.constants.MongoCollectionNameConstants;
import com.ytjj.qmyx.data.model.mongo.ClickStatistics;
import com.ytjj.qmyx.data.service.ClickStatisticsService;
//import com.ytjj.qmyx.data.service.KafkaSendService;
import com.ytjj.qmyx.data.util.CalendarUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * 
 *  *
 **/
@Slf4j
@Service
public class ClickStatisticsServiceImpl implements ClickStatisticsService {

    @Resource
    private MongoTemplate mongoTemplate;

//    @Autowired
//    private KafkaSendService kafkaSendService;

    @Autowired
    private RedisService redisService;

    /**
     * 页面路径CODE
     */
    private static Map<String, String> clickMap = null;


    @Override
    public void clickStatistics(ClickStatisticsDto clickStatisticsDto) {
        ClickStatistics clickStatistics = new ClickStatistics();
        BeanUtils.copyProperties(clickStatisticsDto, clickStatistics);
        String dayTime = CalendarUtil.dateFormat(clickStatistics.getCreateTime());
        Integer hourTime = Integer.parseInt(CalendarUtil.dateFormatHour(clickStatistics.getCreateTime()));
        clickStatistics.setDayTime(dayTime);
        clickStatistics.setHourTime(hourTime);
        if (StringUtils.isEmpty(clickStatistics.getSeq_id())) {
            clickStatistics.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(clickStatistics.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            clickStatistics.setCur_date(sdf.format(new Date()));
        }
        //是否新用户
//        boolean isNewUser = mongoTemplate.exists(Query.query(Criteria.where("uuid").is(clickStatisticsDto.getUuid())), ClickStatistics.class);
//        clickStatistics.setIsNewUser(isNewUser ? 0 : 1);

        mongoTemplate.save(clickStatistics);

        mongoTemplate.save(clickStatistics, MongoCollectionNameConstants.CLICK + dayTime);

        //获取自增主键
        Long incrementId = redisService.getIncrementId(RedisKeyConstant.CLICK_KAFKA_OUT);
        clickStatistics.setIncrementId(incrementId);
        clickStatistics.setCur_timestamp(System.currentTimeMillis());

        //发送到kafka
//        try{
//            log.debug("clickStatisticsDto={}", JSONObject.toJSONString(clickStatistics));
//            boolean sendStatus=kafkaSendService.sendClickMsg(JSONObject.toJSONString(clickStatistics));
//            log.debug("sendClickStatus={}",true);
//        }catch (Exception e){
//            log.error("发送到kafka错误",e);
//        }
    }


}
